package com.ulic.customerManApplication.usermodule.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.commonmodule.bo.Function;
import com.ulic.customerManApplication.commonmodule.bo.Menu;
import com.ulic.customerManApplication.usermodule.bo.Permission;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.usermodule.dao.PermissionDao;
@Component("permissionDao")
public class PermissionDaoImpl extends BaseDao implements PermissionDao {

	@SuppressWarnings("unchecked")
	public void setUserPermission(String userid, int[] objectids)
			throws Exception {
		//删除原有权限
		String hql = "from Permission p where p.user = ?";
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query q = session.createQuery(hql);
		q.setParameter(0, userid);
		List<Permission> list =q.list();
		for (Permission permission : list) {
			hibernateTemplate.delete(permission);
		}
		//添加新的权限
		if(objectids != null) {
			for(int i = 0; i < objectids.length; i++) {
				Permission permission = new Permission();
				permission.setObjectid(objectids[i]);
				User user = (User)hibernateTemplate.load(User.class, userid);
				permission.setUser(user);
				hibernateTemplate.save(permission);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public int[] getUserPermission(String userid) throws Exception {
		String hql = "from Permission p where p.user = "+Integer.parseInt(userid);
		List<Permission> list = hibernateTemplate.find(hql);
		int[] objectids = new int[(list.size())];
		for (int i = 0; i < list.size(); i++) {
			objectids[i] = list.get(i).getObjectid();
		}
		return objectids;
	}

	@SuppressWarnings("unchecked")
	public List<Menu> getMenuPermissionList(int userOid) throws Exception {
		String hql = "from Menu m where m.oid in (select p.objectid from Permission p where p.user =" + userOid + ")  order by m.oid";
		return hibernateTemplate.find(hql);
	}

	@Override
	@SuppressWarnings("unchecked")
	public Map<String, String> getFuncionPermissionMap(int userOid)
			throws Exception {
		@SuppressWarnings("unused")
		User user = (User)hibernateTemplate.load(User.class, userOid);
		String hql = "from Function f  where f.oid in (select p.objectid from Permission p where p.user =" + userOid + ")";
		List<Function> functionList = hibernateTemplate.find(hql);
		Map<String, String> map = new HashMap<String, String>();
		for (Function function : functionList) {
			map.put(function.getCode(), "1");
		}
		return map;
	}

	@Override
	@SuppressWarnings("unchecked")
	public int[] getUserPermissionList(int userOid) throws Exception {
		@SuppressWarnings("unused")
		User user = (User)hibernateTemplate.load(User.class, userOid);
		String hql = "from User u  where u.oid in (select p.objectid from Permission p where p.user =" + userOid + ")";
		//公司领导和系统管理员拥有所有的权限
		
		List<User> userList = hibernateTemplate.find(hql);
		int[] puser = new int[userList.size()];
		for (int i = 0; i < userList.size(); i++) {
			puser[i] = Integer.parseInt(userList.get(i).getUm_user_id());
		}
		return puser;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> getAdminMenuPermissionList() throws Exception {
		String hql = "from Menu m where m.oid = 1";
		return hibernateTemplate.find(hql);
	}

}
